﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Skynet.BusinessEntities;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Skynet.BusinessLogic.DataAccess
{
    public class ExamByCustomerRepository : MasterRepository<BusinessEntities.ExamByCustomer>,  IExamByCustomerRepository
    {
        public ExamByCustomerRepository(IUnityContainer container) : base(container)
        { 

        }
        public override void Add(BusinessEntities.ExamByCustomer item)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            ctx.ExamByCustomers.AddObject(item);
            ctx.SaveChanges();
            item.AcceptChanges();
        }
        public override void Update(ExamByCustomer item)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var original = (from c in ctx.ExamByCustomers where c.Id == item.Id select c).FirstOrDefault();
            if (original == null) throw new System.Data.DeletedRowInaccessibleException("This record have been deleted by another user.");
            ctx.ExamByCustomers.ApplyCurrentValues(item);
            ctx.SaveChanges();
            item.AcceptChanges();
        }
        public override ExamByCustomer GetOne(object key)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = (from c in ctx.ExamByCustomers.Include("Exam") where c.Id == (Guid)key select c).SingleOrDefault();
            return query;  
        }
        public IEnumerable<BusinessEntities.ExamByCustomer> GetExamByCustomer(Guid CustomerId)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = (from c in ctx.ExamByCustomers where c.CustomerId == CustomerId select c);
            return query.ToList();  
        }
        public IEnumerable<BusinessEntities.ExamByCustomer> GetRuntimeByCustomer(Guid CustomerId)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();            
            
            var query = (from c in ctx.ExamByCustomers.Include("Exam").Include("Exam.ExamCategory") where c.CustomerId == CustomerId select c);
            
            return query.ToList();  
        }

        public void UpdateScore(Guid ExamByCustomerId, double score)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var original = (from c in ctx.ExamByCustomers where c.Id == ExamByCustomerId select c).FirstOrDefault();
            if (original == null) throw new System.Data.DeletedRowInaccessibleException("This record have been deleted by another user.");
            original.Score = score;
            ctx.SaveChanges();            
        }
        public void UpdateCurrentTime(Guid ExamByCustomerId)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var original = (from c in ctx.ExamByCustomers where c.Id == ExamByCustomerId select c).FirstOrDefault();
            if (original == null) throw new System.Data.DeletedRowInaccessibleException("This record have been deleted by another user.");
            original.CurrentTime = DateTime.Now;
            ctx.SaveChanges();            
        }
        public double CountActiveExams(Guid CustomerId)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            return (from c in ctx.ExamByCustomers where c.CustomerId == CustomerId && c.Status == (short)BusinessEntities.ExamActivationStatus.Active && DateTime.Now >= c.ValidFrom  && DateTime.Now <= c.ValidTo select c).Count();
        }
        public double CountRunningExams(Guid CustomerId)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            return (from c in ctx.ExamByCustomers where c.CustomerId == CustomerId && c.Status == (short)BusinessEntities.ExamActivationStatus.Inprogress select c).Count();
        }
        public double CountCompleteExams(Guid CustomerId)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            return (from c in ctx.ExamByCustomers where c.CustomerId == CustomerId && c.Status == (short)BusinessEntities.ExamActivationStatus.Finish select c).Count();
        }
        public BusinessEntities.ExamByCustomer GetRuntimeByCustomerExamId(Guid CustomerExamId)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var original = (from c in ctx.ExamByCustomers.Include("Exam").Include("Exam.ExamCategory") where c.Id == CustomerExamId select c).FirstOrDefault();
            return original;                       
        }
        public void UpdateStatus(Guid ExamByCustomerId, BusinessEntities.ExamActivationStatus examActivationStatus)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var original = (from c in ctx.ExamByCustomers where c.Id == ExamByCustomerId select c).FirstOrDefault();
            if (original == null) throw new System.Data.DeletedRowInaccessibleException("This record have been deleted by another user.");
            original.Status = (short)examActivationStatus;
            ctx.SaveChanges(); 
        }
        public void ValidationStatesSystem()
        { 
            Database db = this.ContainerService.Resolve<Database>("SkynetLibrary");
            var cmd =  db.GetSqlStringCommand("update EC set Status = 1 from ExamByCustomers EC where Status = 2 and @Target > ValidTo");
            db.AddInParameter(cmd, "Target", System.Data.DbType.DateTime, DateTime.Now);
            db.ExecuteNonQuery(cmd);            
        }
    }
}
